package com.dao;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import javax.sql.*;
import COM.ibm.db2.jdbc.app.DB2Driver;

public class DB2Connection {
	/**
	 * Script Name   : <b> DB2Connection</b>
	 * Description   : Functional Test Script
	 * Original Host : WinNT Version 5.1  Build 2600 (S)
	 * 
	 * @author Admin
	 */

	public Connection connection = null;
	public ResultSet resultset = null;
	public Statement statement = null;

	public Connection connect() throws Exception 
	{
		
		Driver driver = new COM.ibm.db2.jdbc.app.DB2Driver(); 
		DriverManager.registerDriver(driver);
		System.out.println("Driver Loaded Successfully ...");
		connection = DriverManager.getConnection("jdbc:db2:DWHDV", 
				"ver", "ver123");					
		if (connection == null)
		{
			System.out.println("connection failed");
		}
		connection.setAutoCommit(true);
		System.out.println("Successfully Connected to DB2...");
		statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 
				ResultSet.CONCUR_UPDATABLE);
		return connection;
	}

	public ResultSet query(String arg) throws SQLException 
	{
		try 
		{	
			statement = connection.createStatement
					(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);	         
			resultset = statement.executeQuery( "Select * from <TABLE_NAME> where <CONDITION>"); 
		}catch (Exception ioe) 
		{
			System.out.println("Query failed: " + arg);
			ioe.printStackTrace();
		}
		return resultset;
	}


	public void close () throws Exception 
	{
		if (connection != null)
		{
			try
			{
				connection.commit();
				connection.close();
			}catch (Exception e)
			{
				System.out.println("Problem in closing DB2 connection: " + e.getMessage());
			}
			connection = null;
		}
	}
	
	public void testMain (Object [] args) {
	
	}

	
}
